<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Summary extends CI_Controller {

	public function index()
	{	
		 //welcome page
		 $d['hello'] ="data";		 
		 $views['_content'] = "ordersummary";
		 $this->template->load('template_landing', $views, $d);
		 
		 $this->load->model('transaction');	 

		 $fName = $this->input->post("Fname"); 
	     $mName = $this->input->post("Mname");
		 $lName = $this->input->post("Lname");
		 $nName = $this->input->post("Nname");
		 $email = $this->input->post("Eadd");
		 if(isset($_COOKIE['fb_mobtel'])) $msisdn = $_COOKIE['fb_mobtel'];
		 $otherCont = $this->input->post("Othercont");
date_default_timezone_set('Asia/Manila');
	 		 $birthdate = date("Y-m-d",strtotime($this->input->post("birthday")));
	     $street = $this->input->post("Address");
		 if(isset($_COOKIE['fb_area'])) $area = $_COOKIE['fb_area'];
	     if(isset($_COOKIE['fb_town'])) $town = $_COOKIE['fb_town'];
		 if(isset($_COOKIE['fb_province'])) $province = $_COOKIE['fb_province'];
		 if(isset($_COOKIE['fb_postalcode'])) $postalCode = $_COOKIE['fb_postalcode'];
		 if(isset($_COOKIE['fb_gadgetid'])) $gadgetId = $_COOKIE['fb_gadgetid'];
	     if(isset($_COOKIE['fb_promoid'])) $promoId = $_COOKIE['fb_promoid'];
		 if(isset($_COOKIE['fb_globestoreid'])) $storeId = $_COOKIE['fb_globestoreid'];
		 
	     if(isset($_COOKIE['fb_mode'])){
		    if($_COOKIE['fb_mode']=="edit"){
			   if($fName!="")
		          $this->transaction->saveUserProfileToDB($fName, $mName, $lName, $nName, $email, $msisdn, $otherCont, $birthdate, $street, $area, $town, $province, $postalCode, $gadgetId, $promoId);
			   else 
			      $this->transaction->saveStoreIdToDB($storeId);
			}
			else $this->transaction->saveStoreIdToDB($storeId);
		 }
		 else $this->transaction->saveStoreIdToDB($storeId);
	}

	public function sendPlanCode($mobtel = "")
	{	
		$mobtel = ($mobtel!="") ? urldecode($mobtel) : "";
		
		$this->load->model('transaction');	 
		$userProfile = $this->transaction->retrieveUserProfileToDB($mobtel);
		
		//print_r($userProfile[0]);
		
		if($userData = $userProfile[0])
		{
			$nName = $userData['nName'];	// not passed in api
			
			//$subsObj = new SubscriberObj();
			$fName = $userData['fName'];
			$mName = $userData['mName'];
			$lName = $userData['lName'];
			$email = $userData['email'];
			$msisdn = $mobtel;
			$birthdate = $userData['birthdate'];
			$streetNum = $userData['street'];
			$area = $userData['area'];
			$town = $userData['town'];
			$province = $userData['province'];
			$postalCode = $userData['postalCode'];
			$gadgetId = $userData['gadgetId'];
			$promoId = $userData['promoId'];
			
			$dt = new DateTime($userData['commitmentPeriod']);
			$commitmentPeriod = $dt->format("m/d/Y");
			
			$storeId = "1";
			$channelId = "1";
			
			// submit now to API;
			$this->load->library('Maitaidata');
			//$regReturn = $this->maitaidata->registerSubscriber($subsObj);
			$regReturn = $this->maitaidata->registerSubscriber($fName, $mName, $lName, $email, $mobtel, $birthdate, $streetNum, $area, $town, $province, $postalCode, $gadgetId, $promoId, $commitmentPeriod, $storeId, $channelId);
			
			//print_r($regReturn);
			$resp = $regReturn->obj;
			// if true will send 0 print plan code
			if($resp->responseCode==0)
			{
				$planCode = $resp->planCode;
				// save plan code to db
				$update = $this->transaction->updatePlanCode($mobtel, $planCode);
				if($update) {
					// return json with plancode
					$return['status'] = 0;
					$return['planCode'] = $resp->planCode;
					$return['name'] = ($nName=="") ? $fName : $nName;
				}
			} else {
				// return error registering
				$return['status'] = -1;
				$return['description'] = $resp->responseDesc;
			}
			
			die(json_encode($return));
			//print_r($subsObj);
		}
	}
	
	public function retrieveUserProfileToDB($mobtel = ""){
		$this->load->model('transaction');
		$mobtel = ($mobtel!="") ? urldecode($mobtel) : "";
		$userProfile = $this->transaction->retrieveUserProfileToDB($mobtel);
		//print_r(json_encode($userProfile));
		print_r($userProfile);
		die();
	}
}